import IntegrationTable, {
	IntegrationTableProps,
} from "@/components/IntegrationTable";
import type { ProColumns, ActionType } from "@ant-design/pro-table";
import React, { useEffect, useState, useRef } from "react";
import { Card, Tabs, Row, Statistic, Col, Descriptions } from "antd";
import { DynamicFormItem } from "@/components/DynamicForm";
import { arrayToObject, getArray, getString } from "phinney-toolkit";
import { getPartner } from "./service";
// @page 商家
const Business: React.FC<any> = () => {
	// 组件是否已经卸载
	let isUnMounted = false;
	const actionRef = useRef<ActionType>();
	const [applyStatus, setApplyStatus] = useState<any>("10");
	const [partner, setPartner] = useState<any>({});
	const [stateNum, setStateNum] = useState<any>(20);

	const classList = [
		{ label: "通过", value: 20 },
		{ label: "拒绝", value: 30 },
	];

	const expandType = [
		{ label: "普通推广员", value: 1 },
		{ label: "社区推广员", value: 2 },
	];
	const getPartnerList = async () => {
		const res = await getPartner();
		setPartner({
			auditRejection: res?.data.auditRejection,
			auditSucess: res?.data.auditSucess,
			waitAudit: res?.data.waitAudit,
		});
	};

	// 表格项
	const columns: ProColumns[] = [
		{
			title: "用户名称",
			dataIndex: "userName",
			hideInTable: true,
		},
		{
			title: "用户手机号",
			dataIndex: "phone",
			hideInTable: true,
		},
		{
			title: "用户ID",
			dataIndex: "id",
			search: false,
		},
		{
			title: "用户信息",
			dataIndex: "userName",
			search: false,
		},
		{
			title: "用户手机号",
			dataIndex: "applyUserPhone",
			search: false,
		},
		{
			title: "申请小区",
			dataIndex: "communityName",
			search: false,
		},
		{
			title: "申请类型",
			dataIndex: "partnerType",
			search: false,
			valueEnum: {
				20: "社区推广人",
				10: "普通推广人",
			},
		},
		{
			title: "状态",
			dataIndex: "applyStatus",
			search: false,
			valueEnum: {
				20: "审核通过",
				30: "审核失败",
				10: "待审核",
			},
		},
		{
			title: "创建时间",
			dataIndex: "createTime",
			search: false,
		},
	];

	const getFormItems = (vm: any, isEdit?: boolean): DynamicFormItem[] => {
		const { coupon } = vm.state;
		return [
			{
				type: "text",
				label: "推广员信息",
				name: "communityCode",
				fieldProps: {
					disabled: true,
				},
			},
			{
				type: "text",
				label: "所属小区",
				name: "communityAddress",
			},
			{
				type: "text",
				label: "推广员类型",
				name: "communityBuildingNum",
			},
			{
				type: "text",
				label: "加入时间",
				name: "communityPeopleNum",
				fieldProps: {
					disabled: true,
				},
			},
			{
				type: "text",
				label: "推广人数",
				name: "lon",
				fieldProps: {
					disabled: true,
				},
			},
			{
				type: "text",
				label: "推广收益",
				name: "lat",
				fieldProps: {
					disabled: true,
				},
			},
			{
				type: "text",
				label: "推广收益上线",
				name: "lat",
				fieldProps: {
					disabled: true,
				},
			},
			{
				type: "text",
				label: "已提现收益",
				name: "lat",
				fieldProps: {
					disabled: true,
				},
			},
		];
	};

	// 一体化表格props
	const tablePorps: IntegrationTableProps = {
		rowKey: "id",
		scroll: { x: 1000 },
		columns,
		actionRef,
		listProps: {
			url: "/partner/page-audit-list",
			method: "GET",
			requestType: "params",
			paramsHandle: (params: any) => {
				return {
					...params,
					applyStatus,
				};
			},
		},
		handleOperating: (record: any) => [
			...(applyStatus !== "30"
				? [
						{
							type: "modal",
							props: {
								url: "/partner/aduit",
								method: "POST",
								btnText: "推广员审核",
								modalProps: {
									title: "推广员审核",
									formProps: {
										labelCol: { span: 5 },
									},
									formItems: [
										{
											type: "select",
											name: "state",
											label: "审核状态",
											options: classList,
											required: true,
											fieldProps: {
												onSelect: (option: any, vm: any) => {
													record.showType = option;
													console.log(option, vm, record);
													// vm.setState({ stateType: vm });
													// setStateNum(option);
												},
											},
										},
										{
											type: "select",
											name: "type",
											label: "推广类型",
											options: expandType,
											required: true,
										},
									],
									formValuesHandle: (record: any) => {
										return {
											...record,
										};
									},
								},
							},
						},
				  ]
				: []),

			// ...items,
		],
		tableBefore: () => {
			return (
				<div>
					<Card style={{ marginBottom: 20 }}>
						<Row>
							<Col span={6}>
								<Statistic title="待审核" value={partner.waitAudit} />
							</Col>
							<Col span={6}>
								<Statistic title="已审核" value={partner.auditSucess} />
							</Col>
							<Col span={6}>
								<Statistic title="已拒绝" value={partner.auditRejection} />
							</Col>
						</Row>
					</Card>
					<Card>
						<Tabs
							onChange={(activeKey) => {
								setApplyStatus(activeKey);
								actionRef.current?.reload();
							}}
						>
							<Tabs.TabPane tab="待审核" key="10"></Tabs.TabPane>
							<Tabs.TabPane tab="已通过" key="20"></Tabs.TabPane>
							<Tabs.TabPane tab="已拒绝" key="30"></Tabs.TabPane>
						</Tabs>
					</Card>
				</div>
			);
		},
	};

	// 初始化加载数据
	useEffect(() => {
		getPartnerList();
		return () => {
			isUnMounted = true;
		};
	}, []);

	return <IntegrationTable {...tablePorps} />;
};

export default Business;
